package com.dzl.util;

public class JsonResult<T>{

	//返回数据
    private T data;
    //错误码
    private Integer code;
    //描述信息
    private String msg;
    public JsonResult(){

    }
    public JsonResult(T data,HttpCode httpCode) {
        this.data = data;
        this.code = httpCode.getCode();
        this.msg = httpCode.getMsg();
    }
    public JsonResult(T dataObject,Integer code,String msg) {
    	this.data = dataObject;
    	this.code = code;
    	this.msg = msg;
    }

    public static <T> JsonResult<T>  success(){
        return new JsonResult<>(null, HttpCode.SUCCESS);
    }

    public  static <T> JsonResult<T> success(T object){
        return new JsonResult<>(object, HttpCode.SUCCESS);
    }
    //逻辑校验错误
    public static <T> JsonResult<T> fail(){
        return new JsonResult<>(null,HttpCode.FAILURE);
    }
    public static <T> JsonResult<T> fail(Integer code, String msg) {
        return new JsonResult<>(null, code, msg);
    }
    public static <T> JsonResult<T> failWithMsg(String msg){
        HttpCode failure = HttpCode.FAILURE;
        failure.setMsg(msg);
        return new JsonResult<>(null,failure);
    }
    //参数校验错误
    public static <T> JsonResult<T> paramErr(){
        return new JsonResult<>(null, HttpCode.PARAM_ERR);
    }

    public static <T> JsonResult<T> paramErr(T object){
        return new JsonResult<>(object, HttpCode.PARAM_ERR);
    }

    public static <T> JsonResult<T> paramErrWithMsg(String msg){
        HttpCode paramErr = HttpCode.PARAM_ERR;
        paramErr.setMsg(msg);
        return new JsonResult<>(null,paramErr);
    }

    public static <T> JsonResult<T> paramErrWithMsgAndCode(Integer code, String msg){
        HttpCode paramErr = HttpCode.PARAM_ERR;
        paramErr.setMsg(msg);
        paramErr.setCode(code);
        return new JsonResult<>(null,paramErr);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
